home *** CD-ROM | disk | FTP | other *** search
/ Sprite 1984 - 1993 / Sprite 1984 - 1993.iso / src / cmds / gdb.new / gdb-4.0 / bfd / host-aout.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-08-24  |  11.0 KB  |  339 lines

  1. /* BFD backend for local host's a.out binaries
  2.    Copyright (C) 1990-1991 Free Software Foundation, Inc.
  3.    Written by Cygnus Support.  Probably John Gilmore's fault.
  4.  
  5. This file is part of BFD, the Binary File Descriptor library.
  6.  
  7. This program is free software; you can redistribute it and/or modify
  8. it under the terms of the GNU General Public License as published by
  9. the Free Software Foundation; either version 2 of the License, or
  10. (at your option) any later version.
  11.  
  12. This program is distributed in the hope that it will be useful,
  13. but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  15. GNU General Public License for more details.
  16.  
  17. You should have received a copy of the GNU General Public License
  18. along with this program; if not, write to the Free Software
  19. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
  20.  
  21. #include <ansidecl.h>
  22. #include <sysdep.h>
  23. #include "bfd.h"
  24. #include "libbfd.h"
  25.  
  26. #include <a.out.h>
  27. #include "libaout.h"           /* BFD a.out internal data structures */
  28.  
  29. #include "trad-core.h"        /* Traditional Unix core files */
  30.  
  31. /*======== This next section is stolen from ../include/a.out.gnu.h
  32.   ======== for all the losing Unix systems that don't provide these
  33.   ======== macros.  
  34.  
  35.   When porting to a new system, you must supply:
  36.  
  37.     HOST_PAGE_SIZE
  38.     HOST_SEGMENT_SIZE
  39.     HOST_MACHINE_ARCH    (optional)
  40.     HOST_MACHINE_MACHINE    (optional)
  41.     HOST_TEXT_START_ADDR
  42.     HOST_STACK_END_ADDR
  43.  
  44.   in the ../include/h-systemname.h file.  */
  45.  
  46. #define    PAGE_SIZE    HOST_PAGE_SIZE
  47. #define    SEGMENT_SIZE    HOST_SEGMENT_SIZE
  48. #define    TEXT_START_ADDR    HOST_TEXT_START_ADDR
  49. #define    STACK_END_ADDR    HOST_STACK_END_ADDR
  50.  
  51. /*======== Stolen section begins below.  =================================*/
  52.  
  53. #define    a_info    a_magic        /* Old traditional Unix */
  54.  
  55. #define N_MAGIC(exec) ((exec).a_info & 0xffff)
  56. #define N_SET_MAGIC(exec, magic) \
  57.     ((exec).a_info = (((exec).a_info & 0xffff0000) | ((magic) & 0xffff)))
  58.  
  59. /* Virtual Address of text segment from the a.out file.  For OMAGIC,
  60.    (almost always "unlinked .o's" these days), should be zero.
  61.    For linked files, should reflect reality if we know it.  */
  62.  
  63. #ifndef N_TXTADDR
  64. #define N_TXTADDR(x)    (N_MAGIC(x)==OMAGIC? 0 : TEXT_START_ADDR)
  65. #endif
  66.  
  67. #ifndef N_BADMAG
  68. #define N_BADMAG(x)      (N_MAGIC(x) != OMAGIC        \
  69.             && N_MAGIC(x) != NMAGIC        \
  70.               && N_MAGIC(x) != ZMAGIC)
  71. #endif
  72.  
  73. /* This complexity is for encapsulated COFF support */
  74. #ifndef _N_HDROFF
  75. #define _N_HDROFF(x)    (SEGMENT_SIZE - sizeof (struct exec))
  76. #endif
  77.  
  78. #ifndef N_TXTOFF
  79. #define N_TXTOFF(x)    (N_MAGIC(x) == ZMAGIC ?    \
  80.                 _N_HDROFF((x)) + sizeof (struct exec) :    \
  81.                 sizeof (struct exec))
  82. #endif
  83.  
  84.  
  85. #ifndef N_DATOFF
  86. #define N_DATOFF(x)    ( N_TXTOFF(x) + (x).a_text )
  87. #endif
  88.  
  89. #ifndef N_TRELOFF
  90. #define N_TRELOFF(x)    ( N_DATOFF(x) + (x).a_data )
  91. #endif
  92.  
  93. #ifndef N_DRELOFF
  94. #define N_DRELOFF(x)    ( N_TRELOFF(x) + (x).a_trsize )
  95. #endif
  96.  
  97. #ifndef N_SYMOFF
  98. #define N_SYMOFF(x)    ( N_DRELOFF(x) + (x).a_drsize )
  99. #endif
  100.  
  101. #ifndef N_STROFF
  102. #define N_STROFF(x)    ( N_SYMOFF(x) + (x).a_syms )
  103. #endif
  104.  
  105. /* Address of text segment in memory after it is loaded.  */
  106. #ifndef N_TXTADDR
  107. #define    N_TXTADDR(x)    0
  108. #endif
  109.  
  110. #ifndef N_DATADDR
  111. #define N_DATADDR(x) \
  112.     (N_MAGIC(x)==OMAGIC? (N_TXTADDR(x)+(x).a_text) \
  113.      :  (SEGMENT_SIZE + ((N_TXTADDR(x)+(x).a_text-1) & ~(SEGMENT_SIZE-1))))
  114. #endif
  115.  
  116. /* Address of bss segment in memory after it is loaded.  */
  117. #define N_BSSADDR(x) (N_DATADDR(x) + (x).a_data)
  118.  
  119.  
  120. static bfd_target *NAME(host_aout,callback) ();
  121.  
  122. /*SUPPRESS558*/
  123. /*SUPPRESS529*/
  124.  
  125. bfd_target *
  126. DEFUN(NAME(host_aout,object_p), (abfd),
  127.      bfd *abfd)
  128. {
  129.   unsigned char magicbuf[4];    /* Raw bytes of magic number from file */
  130.   unsigned long magic;        /* Swapped magic number */
  131.  
  132.   bfd_error = system_call_error;
  133.  
  134.   if (bfd_read ((PTR)magicbuf, 1, sizeof (magicbuf), abfd) !=
  135.       sizeof (magicbuf))
  136.     return 0;
  137.   magic = bfd_h_get_32 (abfd, magicbuf);
  138.  
  139.   if (N_BADMAG (*((struct exec *) &magic))) return 0;
  140.  
  141.   return NAME(aout,some_aout_object_p) (abfd, NAME(host_aout,callback));
  142. }
  143.  
  144. /* Set parameters about this a.out file that are machine-dependent.
  145.    This routine is called from NAME(some_aout_object_p) just before it returns.
  146.    */
  147.  
  148. static bfd_target *
  149. DEFUN(NAME(host_aout,callback), (abfd),
  150.      bfd *abfd)
  151. {
  152.   /* exec_hdr (abfd), a "struct internal_exec *", is just an abstraction,
  153.      as far as the BFD a.out layer cares.  We use it as a "struct exec *".
  154.      This routine moves any data from the exec header,
  155.      which is needed by the BFD code, out to places known to BFD.  This
  156.      allows the rest of the BFD code to not know or care about the structure
  157.      of exec_hdr (abfd).  */
  158.   struct exec *execp = (struct exec *)exec_hdr (abfd);
  159.  
  160.   /* The virtual memory addresses of the sections */
  161.   obj_datasec (abfd)->vma = N_DATADDR(*execp);
  162.   obj_bsssec (abfd)->vma = N_BSSADDR(*execp);
  163.   obj_textsec (abfd)->vma = N_TXTADDR(*execp);
  164.  
  165.   /* The file offsets of the sections */
  166.   obj_textsec (abfd)->filepos = N_TXTOFF(*execp);
  167.   obj_datasec (abfd)->filepos = N_DATOFF(*execp);
  168.  
  169.   /* The file offsets of the relocation info */
  170.   obj_textsec (abfd)->rel_filepos = N_TRELOFF(*execp);
  171.   obj_datasec (abfd)->rel_filepos = N_DRELOFF(*execp);
  172.  
  173.   /* The file offsets of the string table and symbol table.  */
  174.   obj_str_filepos (abfd) = N_STROFF (*execp);
  175.   obj_sym_filepos (abfd) = N_SYMOFF (*execp);
  176.  
  177. #ifdef HOST_MACHINE_ARCH
  178.   abfd->obj_arch = HOST_MACHINE_ARCH;
  179. #endif
  180. #ifdef HOST_MACHINE_MACHINE
  181.   abfd->obj_machine = HOST_MACHINE_MACHINE;
  182. #endif
  183.  
  184.   obj_reloc_entry_size (abfd) = sizeof (struct relocation_info);
  185.   return abfd->xvec;
  186. }
  187.  
  188.  
  189. boolean
  190. DEFUN(NAME(host_aout,mkobject), (abfd),
  191.      bfd *abfd)
  192. {
  193.   /* This struct is just for allocating two things with one zalloc, so
  194.      they will be freed together, without violating alignment constraints. */
  195.   struct aout_exec {
  196.     struct aoutdata    aoutdata;
  197.     struct exec    exec;
  198.   } *rawptr;
  199.  
  200.   bfd_error = system_call_error;
  201.  
  202.   /* Use an intermediate variable for clarity */
  203.   rawptr = (struct aout_exec *)bfd_zalloc (abfd, sizeof (struct aout_exec));
  204.  
  205.   if (rawptr == NULL) {
  206.     bfd_error = no_memory;
  207.     return false;
  208.   }
  209.  
  210.   set_tdata (abfd, &rawptr->aoutdata);
  211.   /* exec_hdr (abfd), a "struct internal_exec *", is just an abstraction,
  212.      as far as the BFD a.out layer cares.  We use it as a "struct exec *".  */
  213.   exec_hdr (abfd) = (struct internal_exec *) &rawptr->exec;
  214.  
  215.   /* For simplicity's sake we just make all the sections right here. */
  216.  
  217.   obj_textsec (abfd) = (asection *)NULL;
  218.   obj_datasec (abfd) = (asection *)NULL;
  219.   obj_bsssec (abfd) = (asection *)NULL;
  220.   bfd_make_section (abfd, ".text");
  221.   bfd_make_section (abfd, ".data");
  222.   bfd_make_section (abfd, ".bss");
  223.  
  224.   return true;
  225. }
  226.  
  227. /* Write an object file in host a.out format.
  228.    Section contents have already been written.  We write the
  229.    file header, symbols, and relocation.  */
  230.  
  231. boolean
  232. DEFUN(NAME(host_aout,write_object_contents), (abfd),
  233.      bfd *abfd)
  234. {
  235. /* This works because we are on the host system */
  236. #define    EXEC_BYTES_SIZE        (sizeof (struct exec))
  237. #define    EXTERNAL_LIST_SIZE    (sizeof (struct nlist))
  238.   size_t data_pad = 0;
  239.   unsigned char exec_bytes[EXEC_BYTES_SIZE];
  240.   struct exec *execp = (struct exec *)exec_hdr (abfd);
  241.  
  242.   execp->a_text = obj_textsec (abfd)->size;
  243.  
  244.   WRITE_HEADERS (abfd, execp);
  245.   return true;
  246. }
  247.  
  248. /* We use BFD generic archive files.  */
  249. #define    aout_32_openr_next_archived_file    bfd_generic_openr_next_archived_file
  250. #define    aout_32_generic_stat_arch_elt        bfd_generic_stat_arch_elt
  251. #define    aout_32_slurp_armap            bfd_slurp_bsd_armap
  252. #define    aout_32_slurp_extended_name_table    bfd_true
  253. #define    aout_32_write_armap            bsd_write_armap
  254. #define    aout_32_truncate_arname            bfd_bsd_truncate_arname
  255. /* #define aout_32_machine_type             sunos_machine_type */
  256.  
  257. /* Traditional Unix core files with upage */
  258. #define    aout_32_core_file_failing_command     trad_unix_core_file_failing_command
  259. #define    aout_32_core_file_failing_signal    trad_unix_core_file_failing_signal
  260. #define    aout_32_core_file_matches_executable_p    trad_unix_core_file_matches_executable_p
  261.  
  262.  
  263. #define    aout_64_openr_next_archived_file    bfd_generic_openr_next_archived_file
  264. #define    aout_64_generic_stat_arch_elt        bfd_generic_stat_arch_elt
  265. #define    aout_64_slurp_armap            bfd_slurp_bsd_armap
  266. #define    aout_64_slurp_extended_name_table    bfd_true
  267. #define    aout_64_write_armap            bsd_write_armap
  268. #define    aout_64_truncate_arname            bfd_bsd_truncate_arname
  269. /* #define aout_64_machine_type             sunos_machine_type */
  270.  
  271. #define    aout_64_core_file_failing_command     trad_unix_core_file_failing_command
  272. #define    aout_64_core_file_failing_signal    trad_unix_core_file_failing_signal
  273. #define    aout_64_core_file_matches_executable_p    trad_unix_core_file_matches_executable_p
  274.  
  275. #define aout_64_bfd_debug_info_start        bfd_void
  276. #define aout_64_bfd_debug_info_end        bfd_void
  277. #define aout_64_bfd_debug_info_accumulate    bfd_void
  278.  
  279. #define aout_32_bfd_debug_info_start        bfd_void
  280. #define aout_32_bfd_debug_info_end        bfd_void
  281. #define aout_32_bfd_debug_info_accumulate    (PROTO(void,(*),(bfd*, struct sec *))) bfd_void
  282.  
  283.  
  284. /* We implement these routines ourselves, rather than using the generic
  285.    a.out versions.  */
  286. #define    aout_write_object_contents    host_write_object_contents
  287.  
  288. bfd_target host_aout_big_vec =
  289.   {
  290.     "a.out-host-big",
  291.     bfd_target_aout_flavour_enum,
  292.     true,            /* target byte order */
  293.     true,            /* target headers byte order */
  294.     (HAS_RELOC | EXEC_P |    /* object flags */
  295.      HAS_LINENO | HAS_DEBUG |
  296.      HAS_SYMS | HAS_LOCALS | DYNAMIC | WP_TEXT | D_PAGED),
  297.     (SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD | SEC_RELOC), /* section flags */
  298.     ' ',                           /* ar_pad_char */
  299.     16,                               /* ar_max_namelen */
  300.     3,                               /* minimum alignment power */
  301.     _do_getb64, _do_putb64, _do_getb32, _do_putb32, _do_getb16, _do_putb16, /* data */
  302.     _do_getb64, _do_putb64, _do_getb32, _do_putb32, _do_getb16, _do_putb16, /* hdrs */
  303.     
  304.       {_bfd_dummy_target, NAME(host_aout,object_p),
  305.        bfd_generic_archive_p, trad_unix_core_file_p},
  306.       {bfd_false, NAME(host_aout,mkobject),
  307.        _bfd_generic_mkarchive, bfd_false},
  308.       {bfd_false, NAME(host_aout,write_object_contents), /* bfd_write_contents */
  309.        _bfd_write_archive_contents, bfd_false},
  310.     
  311.     JUMP_TABLE(JNAME(aout))
  312. };
  313.  
  314. bfd_target host_aout_little_vec =
  315.   {
  316.     "a.out-host-little",
  317.     bfd_target_aout_flavour_enum,
  318.     false,            /* target byte order */
  319.     false,            /* target headers byte order */
  320.     (HAS_RELOC | EXEC_P |    /* object flags */
  321.      HAS_LINENO | HAS_DEBUG |
  322.      HAS_SYMS | HAS_LOCALS | DYNAMIC | WP_TEXT | D_PAGED),
  323.     (SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD | SEC_RELOC), /* section flags */
  324.     ' ',                           /* ar_pad_char */
  325.     16,                               /* ar_max_namelen */
  326.     3,                               /* minimum alignment power */
  327.     _do_getb64, _do_putb64, _do_getb32, _do_putb32, _do_getb16, _do_putb16, /* data */
  328.     _do_getb64, _do_putb64, _do_getb32, _do_putb32, _do_getb16, _do_putb16, /* hdrs */
  329.     
  330.       {_bfd_dummy_target, NAME(host_aout,object_p),
  331.        bfd_generic_archive_p, trad_unix_core_file_p},
  332.       {bfd_false, NAME(host_aout,mkobject),
  333.        _bfd_generic_mkarchive, bfd_false},
  334.       {bfd_false, NAME(host_aout,write_object_contents), /* bfd_write_contents */
  335.        _bfd_write_archive_contents, bfd_false},
  336.     
  337.     JUMP_TABLE(JNAME(aout))
  338. };
  339.